﻿using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SCABaselineCS.Tests.Reliability
{
    [TestClass]
    public class ImplementsEqualsTests
    {
        public void Snippet_Pass__Obj_To_Obj(object obj1, object obj2)
        {
            var returnValue = Object.Equals(obj1, obj2);
        }
        public class MyObject { }
        public void Snippet_Fail__MyObj_To_Obj(MyObject obj1, object obj2)
        {
            var returnValue = obj1.Equals(obj2);
        }
        public void Snippet_Fail__MyObj_To_MyObj(MyObject obj1, MyObject obj2)
        {
            var returnValue = obj1.Equals(obj2);
        }

        //[TestMethod]
        public void Example_Pass()
        {
            // TODO
        }

        //[TestMethod]
        public void Example_Fail()
        {
            // TODO
        }




        public class MyGeneric<T>
        {
            public bool SetterOccurred { get; set; }
            public T _SetterTestValue;
            public T SetterTest
            {
                set
                {
                    /*
                    ** this is a code snippet that I'd used, which has specifically set off alarms
                    **  concerning whether the object had implemented Equals.
                    ** In practice, such concern should be based on the type of T provided.
                    */
                    
                    if (value == null && _SetterTestValue == null)
                        return;

                    if ((value == null && _SetterTestValue != null) ||
                        (value != null && _SetterTestValue == null) ||
                        (!value.Equals(_SetterTestValue)))
                    {
                        SetterOccurred = true;
                        _SetterTestValue = value;
                    }

                }
            }
        }

        [TestMethod]
        public void GenericExample_Pass_Test1()
        {
            // Arrange
            Guid a = Guid.Parse("11111111-1111-1111-1111-111111111111");
            Guid b = Guid.Parse("22222222-2222-2222-2222-222222222222");

            var instance = new MyGeneric<Guid>();
            instance.SetterTest = a;
            instance.SetterOccurred = false; // clear

            // Act
            instance.SetterTest = b;

            // Assert
            Assert.IsTrue(instance.SetterOccurred);
        }

        [TestMethod]
        public void GenericExample_Pass_Test2()
        {
            // Arrange
            Guid a = Guid.Parse("11111111-1111-1111-1111-111111111111");
            Guid b = Guid.Parse("11111111-1111-1111-1111-111111111111");

            var instance = new MyGeneric<Guid>();
            instance.SetterTest = a;
            instance.SetterOccurred = false; // clear

            // Act
            instance.SetterTest = b;

            // Assert
            Assert.IsFalse(instance.SetterOccurred);
        }

    } // class
} // namespace